/**
 * 
 */
package com.jiezheng.huinong.action;

import java.util.Map;

import com.jiezheng.huinong.dao.ApplyManageDAOImpl;
import com.jiezheng.huinong.dao.IndexManageDAOImpl;
import com.jiezheng.huinong.model.ApplyInfo;
import com.jiezheng.huinong.model.IndexInfo;
import com.jiezheng.huinong.model.OperationStatus;
import com.jiezheng.huinong.util.CommonUtils;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @author Administrator
 * 
 */
public class ApplyManageAction extends ActionSupport {

	private static final long serialVersionUID = -6667525636457351449L;

	// 所有的指标信息
	private ApplyInfo apply = new ApplyInfo();

	// 所有的申请ID列表
	private String idList = "";

	/**
	 * 增加申请信息
	 * 
	 * @return
	 */
	public String addApply() {

		// 增加用户部门
		Map<String, Object> session = ActionContext.getContext().getSession();
		String deptNo = (String) session.get("deptNo");
		apply.setDeptNo(deptNo);

		(new ApplyManageDAOImpl()).addApply(apply);
		return SUCCESS;
	}

	/**
	 * 删除申请信息
	 * 
	 * @return
	 */
	public String deleteApply() {
		(new ApplyManageDAOImpl()).deleteApply(apply.getApplyNo());
		return SUCCESS;
	}

	/**
	 * 修改申请信息表单
	 */
	public String preModifyApply() {
		return SUCCESS;
	}

	/**
	 * 修改申请信息表单
	 */
	public String showApplyDetail() {

		return SUCCESS;
	}

	/**
	 * 修改申请信息
	 * 
	 * @return
	 */
	public String modifyApply() {
		(new ApplyManageDAOImpl()).modifyApply(apply);
		return SUCCESS;
	}

	/**
	 * 预挂接指标显示
	 * 
	 * @return
	 */
	public String preJointIndex() {
		apply = (new ApplyManageDAOImpl())
				.getApplyByApplyNo(apply.getApplyNo());
		return SUCCESS;
	}

	/**
	 * 挂接指标
	 * 
	 * @return
	 */
	public String toJoint() {
		(new ApplyManageDAOImpl()).jointIndex(apply);

		// 消减可用金额
		IndexInfo index = new IndexInfo();
		index.setIndexNo(apply.getIndexNo());
		index.setBudgetItemNo(apply.getBudgetItem());
		index.setBudgetUnitNo(apply.getBudgetUnit());
		index.setSpareMoney(apply.getMoney());
		(new IndexManageDAOImpl()).reduceSpareMoney(index);
		return SUCCESS;
	}

	/**
	 * 取消挂接指标
	 * 
	 * @return
	 */
	public String unJoint() {
		int status = OperationStatus.NO_JONIT;
		String[] idCards = idList.split("[:]");
		for (String idCard : idCards) {
			if (!idCard.isEmpty()) {

				// 增加可用金额
				ApplyInfo applyInfo = (new ApplyManageDAOImpl())
						.getApplyByApplyNo(idCard);
				IndexInfo index = new IndexInfo();
				index.setIndexNo(applyInfo.getIndexNo());
				index.setBudgetItemNo(applyInfo.getBudgetItem());
				index.setBudgetUnitNo(applyInfo.getBudgetUnit());
				index.setSpareMoney(applyInfo.getMoney());
				(new IndexManageDAOImpl()).increaseSpareMoney(index);

				// 解除挂接关系
				ApplyInfo apply = new ApplyInfo();
				apply.setApplyNo(idCard);
				apply.setStatus(status);
				(new ApplyManageDAOImpl()).unJointIndex(apply);
			}
		}
		return SUCCESS;
	}

	/**
	 * 送审申请信息
	 * 
	 * @return
	 */
	public String toAudit() {
		audit(OperationStatus.NO_AUDIT);
		return SUCCESS;
	}

	/**
	 * 审核通过申请信息
	 * 
	 * @return
	 */
	public String approveAudit() {
		audit(OperationStatus.FINAL_AUDITED);
		return SUCCESS;
	}

	/**
	 * 审核通过申请信息
	 * 
	 * @return
	 */
	public String rejectAudit() {
		audit(OperationStatus.REJECTED);
		return SUCCESS;
	}

//	/**
//	 * 最终审核通过申请信息
//	 * 
//	 * @return
//	 */
//	public String approveFinalAudit() {
//		audit(OperationStatus.FINAL_AUDITED);
//		return SUCCESS;
//	}
//
//	/**
//	 * 最终审核不通过申请信息
//	 * 
//	 * @return
//	 */
//	public String rejectFinalAudit() {
//		audit(OperationStatus.REJECTED);
//		return SUCCESS;
//	}

	/**
	 * 提交中国人民银行
	 * 
	 * @return
	 */
	public String submitPay() {
		audit(OperationStatus.SUBMITED);
		return SUCCESS;
	}

	/**
	 * 中国人民银行确认支付
	 * 
	 * @return
	 */
	public String confirmPay() {
		pay(OperationStatus.EXECUTED);
		return SUCCESS;
	}

	public void audit(Integer status) {
		String[] idCards = idList.split("[:]");
		for (String idCard : idCards) {
			if (!idCard.isEmpty()) {
				ApplyInfo apply = new ApplyInfo();
				apply.setApplyNo(idCard);
				apply.setStatus(status);
				(new ApplyManageDAOImpl()).changeStatus(apply);
			}
		}
	}
	
	public void pay(Integer status) {
		String[] idCards = idList.split("[:]");
		String date = CommonUtils.getCurrentDate();
		for (String idCard : idCards) {
			if (!idCard.isEmpty()) {
				ApplyInfo apply = new ApplyInfo();
				apply.setApplyNo(idCard);
				apply.setStatus(status);
				apply.setPayDate(date);
				(new ApplyManageDAOImpl()).changePayStatus(apply);
			}
		}
	}
	


	public ApplyInfo getApply() {
		return apply;
	}

	public void setApply(ApplyInfo apply) {
		this.apply = apply;
	}

	public String getIdList() {
		return idList;
	}

	public void setIdList(String idList) {
		this.idList = idList;
	}

}
